package ibm.websphere.monitor.page;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

/**
 * @copyright www.timotai.net
 * @author 这家伙真懒
 * @date 2016-1-12
 * <pre></pre>
 **/
public abstract class WasPage<T> {
    //默认的每页显示的记录数//
    public static final int DEFAULT_PAGESIZE = 20;

    //默认的传递页码的参数名称
    public static final String DEFAULT_PAGENO_KEY = "pageNo";

    public static final String IDEA_PAGE_TEMPLATE = "/page/global/pagination/#{TYPE}.jsp";

    public static final String PAGENO_REPLACEMENT = "#{pageNo}";

    public static <T> WasPage<T> create(HttpServletRequest request) {
        return WasPage.create(request, null, DEFAULT_PAGESIZE);
    }

    public static <T> WasPage<T> create(
            HttpServletRequest request, int maxResult) {
        return WasPage.create(request, null, maxResult);
    }

    /***/
    public static <T> WasPage<T> create(
            HttpServletRequest request, String pageNoKey) {
        return WasPage.create(request, pageNoKey, WasPage.DEFAULT_PAGESIZE);
    }

    /**
     * @param request   http  request对象
     * @param pageNoKey 传递页码的参数名称 默认为 使用pageNo传递
     * @param maxResult 每页读取的记录条数
     * @return 返回新的分页记录对象
     * <pre>
     * 	实例一个分页对象
     *
     * 示例：
     *
     * HttpServletRequest request;
     * IdeaPageResult     result;
     *
     *  IdeaPage ideaPage = IdeaPage.create(request, "pageNo", 20);
     *  ideaPage.execute(result);
     * </pre>
     */
    public static <T> WasPage<T> create(
            HttpServletRequest request, String pageNoKey, int maxResult) {
        WasPage<T> instance = null;
        instance = new WasPageImpl<T>(request, pageNoKey, maxResult);
        return instance;
    }


    /**
     * 获取总页数
     */
    public abstract int getPageCount();

    /**
     * 获取当前分页结果集所对应的总记录数
     */
    public abstract int getRecordCount();

    /**
     * 获取每页显示的记录条数
     */
    public abstract int getPageSize();

    /**
     * 获取读取记录的开始位置
     */
    public abstract int getOffset();

    /**
     * 获取当前的页码
     */
    public abstract int getPageNo();

    /**
     * 获取当前的URL模板
     */
    public abstract String getPageURL();

    /**
     * 获取传递页码的参数名称
     */
    public abstract String getPageNoKey();

    /**
     * 获取分页的结果集
     */
    public abstract List<T> getDataList();

    /***
     * @param pageResult 分页结果集
     * <pre>
     *   执行建立分页，并将IdeaPage信息对象保存到request中
     *  </pre>
     */
    public abstract void execute(PageResult<T> pageResult);

    public abstract void execute(List<T> dataList, int numberObjects);
}
